{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# 6 Pandas的函数应用\n",
    "# apply()是对行或列进行操作，applymap()是对每个元素进行操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          A         B         C         D\n",
      "0 -0.503286 -1.138264 -0.352311  0.523030\n",
      "1 -1.234153 -1.234137  0.579213 -0.232565\n",
      "2 -1.469474 -0.457440 -1.463418 -1.465730\n",
      "3 -0.758038 -2.913280 -2.724918 -1.562288\n",
      "4 -2.012831 -0.685753 -1.908024 -2.412304\n",
      "          A         B         C         D\n",
      "0  0.503286  1.138264  0.352311  0.523030\n",
      "1  1.234153  1.234137  0.579213  0.232565\n",
      "2  1.469474  0.457440  1.463418  1.465730\n",
      "3  0.758038  2.913280  2.724918  1.562288\n",
      "4  2.012831  0.685753  1.908024  2.412304\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "np.random.seed(42)\n",
    "df = pd.DataFrame(np.random.randn(5,4) - 1, columns=list('ABCD'))\n",
    "print(df)\n",
    "\n",
    "print(np.abs(df)) #绝对值"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-04T04:52:25.721143300Z",
     "start_time": "2024-05-04T04:52:25.668534300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A   -0.503286\n",
      "B   -0.457440\n",
      "C    0.579213\n",
      "D    0.523030\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "# 沿轴0找最大值\n",
    "print(df.apply(lambda x : x.max()))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-04T04:52:28.979886800Z",
     "start_time": "2024-05-04T04:52:28.940403100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "data": {
      "text/plain": "A   -0.503286\nB   -1.138264\nC   -0.352311\nD    0.523030\nName: 0, dtype: float64"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc[0]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-04T04:52:31.789743600Z",
     "start_time": "2024-05-04T04:52:31.751942800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    0.523030\n",
      "1    0.579213\n",
      "2   -0.457440\n",
      "3   -0.758038\n",
      "4   -0.685753\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "# 沿轴1找最大值\n",
    "print(df.apply(lambda x : x.max(), axis=1))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-04T04:52:35.473955600Z",
     "start_time": "2024-05-04T04:52:35.437256Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       0      1      2      3\n",
      "0  -2.04  -1.23  -2.77  -1.95\n",
      "1  -1.07  -0.51  -1.69  -1.30\n",
      "2  -0.02  -4.33   0.38  -0.26\n",
      "3  -0.93  -0.94  -1.22  -1.37\n",
      "4  -1.45  -1.22  -0.40   0.30\n",
      "--------------------------------------------------\n",
      "0    float64\n",
      "1    float64\n",
      "2    float64\n",
      "3    float64\n",
      "dtype: object\n"
     ]
    }
   ],
   "source": [
    "# 使用applymap应用到每个数据\n",
    "print(df.applymap(lambda x : '%.2f' % x))\n",
    "print(\"-\"*50)\n",
    "\n",
    "print(df.dtypes)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-01T05:43:32.132134300Z",
     "start_time": "2024-05-01T05:43:32.071733500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "print(type('%.2f' % 1.3456))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-01T05:44:41.222795200Z",
     "start_time": "2024-05-01T05:44:41.181634100Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 索引排序"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3    10\n",
      "4    11\n",
      "2    12\n",
      "4    13\n",
      "4    14\n",
      "dtype: int64\n",
      "--------------------------------------------------\n",
      "2    12\n",
      "3    10\n",
      "4    11\n",
      "4    13\n",
      "4    14\n",
      "dtype: int64\n",
      "3    10\n",
      "4    11\n",
      "2    12\n",
      "4    13\n",
      "4    14\n",
      "dtype: int64\n",
      "--------------------------------------------------\n",
      "3    10\n",
      "4    11\n",
      "2    12\n",
      "dtype: int64\n",
      "3    10\n",
      "4    11\n",
      "2    12\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "# Series\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "np.random.seed(42)\n",
    "s4 = pd.Series(range(10, 15), index = np.random.randint(5, size=5)) #索引随机生成\n",
    "print(s4)\n",
    "print('-'*50)\n",
    "\n",
    "# 索引排序,sort_index返回一个新的排好索引的series\n",
    "print(s4.sort_index())\n",
    "print(s4)\n",
    "print('-'*50)\n",
    "\n",
    "# s4.loc[0:3]  loc索引值不唯一时直接报错\n",
    "print(s4.iloc[0:3])\n",
    "print(s4[0:3]) #默认用的位置索引"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-01T05:47:42.608024700Z",
     "start_time": "2024-05-01T05:47:42.580303100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [],
   "source": [
    "# s4.loc[1:2] #loc索引值唯一时可以切片"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-01T05:32:40.759724300Z",
     "start_time": "2024-05-01T05:32:40.642009900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          0         3         1         1         0\n",
      "2  0.496714 -0.138264  0.647689  1.523030 -0.234153\n",
      "4 -0.234137  1.579213  0.767435 -0.469474  0.542560\n",
      "0 -0.463418 -0.465730  0.241962 -1.913280 -1.724918\n",
      "1 -0.562288 -1.012831  0.314247 -0.908024 -1.412304\n",
      "3  1.465649 -0.225776  0.067528 -1.424748 -0.544383\n",
      "          0         3         1         1         0\n",
      "4 -0.234137  1.579213  0.767435 -0.469474  0.542560\n",
      "3  1.465649 -0.225776  0.067528 -1.424748 -0.544383\n",
      "2  0.496714 -0.138264  0.647689  1.523030 -0.234153\n",
      "1 -0.562288 -1.012831  0.314247 -0.908024 -1.412304\n",
      "0 -0.463418 -0.465730  0.241962 -1.913280 -1.724918\n"
     ]
    }
   ],
   "source": [
    "# DataFrame\n",
    "\n",
    "np.random.seed(42)\n",
    "df4 = pd.DataFrame(np.random.randn(5, 5),\n",
    "                   index=np.random.randint(5, size=5),\n",
    "                   columns=np.random.randint(5, size=5))\n",
    "print(df4)\n",
    "#轴零是行索引排序\n",
    "df4_isort = df4.sort_index(axis=0, ascending=False)\n",
    "print(df4_isort)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-04T03:55:50.612478100Z",
     "start_time": "2024-05-04T03:55:50.570120100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          0         0         1         1         3\n",
      "2  0.496714 -0.234153  0.647689  1.523030 -0.138264\n",
      "4 -0.234137  0.542560  0.767435 -0.469474  1.579213\n",
      "0 -0.463418 -1.724918  0.241962 -1.913280 -0.465730\n",
      "1 -0.562288 -1.412304  0.314247 -0.908024 -1.012831\n",
      "3  1.465649 -0.544383  0.067528 -1.424748 -0.225776\n"
     ]
    }
   ],
   "source": [
    "# 轴1是列索引排序\n",
    "df4_isort = df4.sort_index(axis=1, ascending=True)\n",
    "print(df4_isort)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-04T03:56:02.483737200Z",
     "start_time": "2024-05-04T03:56:02.431876400Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 6.5 按值排序"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-28T15:58:23.848748Z",
     "start_time": "2024-04-28T15:58:23.811691800Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    0   1   2   3\n",
      "0   6  34  11  98\n",
      "1  52  34  13   4\n",
      "2  48  68  71  42\n",
      "3  43   6  20  17\n",
      "4  43  71  42  89\n",
      "5  31  20   0  55\n",
      "--------------------------------------------------\n",
      "    0   1   2   3\n",
      "0   6  34  11  98\n",
      "4  43  71  42  89\n",
      "5  31  20   0  55\n",
      "2  48  68  71  42\n",
      "3  43   6  20  17\n",
      "1  52  34  13   4\n"
     ]
    }
   ],
   "execution_count": 40
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[16  0  3  4]\n",
      " [66 69 19 89]\n",
      " [59 84 39 45]\n",
      " [79 87 79 50]\n",
      " [ 2 33 71 15]\n",
      " [62 43 45 84]]\n",
      "--------------------------------------------------\n",
      "    0   1   2   3\n",
      "0  16   0   3   4\n",
      "1  66  69  19  89\n",
      "2  59  84  39  45\n",
      "3  79  87  79  50\n",
      "4   2  33  71  15\n",
      "5  62  43  45  84\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "# 按值排序,by后是column的值\n",
    "import random\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "np.random.seed(42)\n",
    "l=[random.randint(0,100) for i in range(24)] #生成24个随机数\n",
    "df4 = pd.DataFrame(np.array(l).reshape(6,4)) #生成6行4列的dataframe\n",
    "print(df4.values) #查看数据,ndarray\n",
    "print('-'*50)\n",
    "\n",
    "print(df4)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-02T14:19:27.122102100Z",
     "start_time": "2024-05-02T14:19:26.532680500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    0   1   2   3\n",
      "1  66  69  19  89\n",
      "5  62  43  45  84\n",
      "3  79  87  79  50\n",
      "2  59  84  39  45\n",
      "4   2  33  71  15\n",
      "0  16   0   3   4\n"
     ]
    }
   ],
   "source": [
    "# 按轴0排序，则by是列名\n",
    "# 比如有一个特征是身高，现在按身高对样本进行排序。其他特征不影响\n",
    "df4_vsort = df4.sort_values(by=3,axis=0, ascending=False) #寻找的是columns里的3,重要\n",
    "print(df4_vsort)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-02T14:20:46.476317500Z",
     "start_time": "2024-05-02T14:20:46.426443200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    1   3   2   0\n",
      "0  91  68  12  95\n",
      "1   8   8  74  62\n",
      "2  96  46  96   0\n",
      "3  86  69  14   7\n",
      "4  57  23  96  82\n",
      "5  81  70  90  30\n"
     ]
    }
   ],
   "source": [
    "# 按轴1排序，则by是行索引名\n",
    "# 没什么用，因为每个样本的特征之间不一定能比较\n",
    "df4_vsort = df4.sort_values(by=3,axis=1, ascending=False) #寻找的是index里的3\n",
    "print(df4_vsort)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-01T05:32:40.824493500Z",
     "start_time": "2024-05-01T05:32:40.702563600Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
